﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WyUnity.Data.Query
{
    public enum SQLOrderSeq
    {
        ASC,
        DESC
    }
    public class OrderClip : IOrderByClip
    {

        public string Field { get; set; }
        public SQLOrderSeq OrderBy { get; set; }
        public string TableAlias { get; set; }
        bool isroot { get; set; }
        public OrderClip(string field)
            : this(string.Empty, field, SQLOrderSeq.ASC)
        {

        }
        public OrderClip(string field, SQLOrderSeq operation)
            : this(string.Empty, field, operation)
        {

        }
        public OrderClip(string tableAlias, string field, SQLOrderSeq order_seq)
        {
            Field = field;
            OrderBy = order_seq;
            TableAlias = tableAlias;
            isroot = true;
        }
        List<IOrderByClip> _orderlist;
        public IOrderByClip Then(IOrderByClip order)
        {
            if (_orderlist == null) _orderlist = new List<IOrderByClip>();
            if (order is OrderClip)
            {
                var o = order as OrderClip;
                o.isroot = false;

                _orderlist.Add(order);
            }
            return this;
        }
        #region IOrderClip 成员

        public string ToSqlClip()
        {
            string r = "[{0}] {1}";
            if (!string.IsNullOrEmpty(TableAlias)) r = "[" + TableAlias + "].";

            r = string.Format(r, Field, OrderToString(OrderBy));
            if (_orderlist != null)
            {
                for (int i = 0; i < _orderlist.Count; i++)
                    r += "," + _orderlist[i].ToSqlClip();
            }
            if (isroot)
                r = " order by " + r;
            return r;
        }

        string OrderToString(SQLOrderSeq o)
        {
            switch (o)
            {
                case SQLOrderSeq.ASC:
                    return "";
                case SQLOrderSeq.DESC:
                    return "DESC";
                default:
                    throw new NotImplementedException();
            }
        }
        #endregion

        public static IOrderByClip None { get { return new NonOrder(); } }
        public List<string> GetAllField()
        {
            var r = new List<string>();
            r.Add(Field);
            if (_orderlist != null)
            {
                foreach (var o in _orderlist)
                {
                    r.AddRange(o.GetAllField());
                }
            }
            return r;
        }

    }

    public class NonOrder : IOrderByClip
    {
        #region IOrderByClip 成员

        public IOrderByClip Then(IOrderByClip clip)
        {
            throw new NotImplementedException();
        }

        public string ToSqlClip()
        {
            return string.Empty;
        }

        #endregion

        public List<string> GetAllField()
        {
            return new List<string>();
        }
    }
}
